package com.kldaby.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Configuration;

@Aspect
@Configuration
public class AspectTest {

    @Pointcut("execution(* greet*(..) )")
    public void excudeService() {}
    //环绕增强与异常增强不能同时使用，否则发生异常时异常增强无法捕获
//    @Around(value = "excudeService()")
//    public void beforeGreeting(ProceedingJoinPoint joinPoint) {
//        System.out.println("How are you");
//        try {
//            Object[] args = joinPoint.getArgs();
//            joinPoint.proceed(args);
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        System.out.println("Welecom you agin");
//    }

//    public void before

    @Before(value = "execution(* serve*(..) )")
    public void beforeService() {
        System.out.println("I am Pleasure to Service you");
    }

    @AfterThrowing(pointcut = "excudeService()",throwing = "ex")
    public void afterThrowing(Throwable ex) {
        StackTraceElement[] traceElements = ex.getStackTrace();
        String className = traceElements[0].getClassName();
        String methodName = traceElements[0].getMethodName();
        int line = traceElements[0].getLineNumber();
        System.out.println("捕获发生的异常，异常位于："+className + "类" + methodName + "方法，第 " + line + "行代码处" +
            "异常信息:" + ex.getLocalizedMessage());
    }

    @AfterReturning(value = "execution(* getMessage())",returning = "msg")
    public void afterReturn(String msg) {
        System.out.println("获得客户发出的消息:" + msg);
    }
}
